package com.admin.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.admin.bean.AdminCpBean;
import com.admin.bean.AdminSpBean;
import com.admin.bean.AdminSpCpSearchBean;
import com.admin.bean.AdminStatFeeBean;
import com.admin.bean.AdminUserBean;
import com.admin.bean.CompanyBean;
import com.admin.bean.GameDataBean;
import com.admin.service.AdminSpService;
import com.admin.service.AdminUserService;
import com.admin.service.CompanyService;
import com.admin.service.GameDataService;
import com.admin.service.ProvinceService;
import com.admin.util.BaseUtil;
import com.admin.util.BillingUtil;
import com.admin.util.DateUtil;
import com.admin.util.PageBean;


@Controller
@RequestMapping("/game")
public class GameDataAction {

	
	@Autowired
	private AdminUserService adminUserService;
	@Autowired private GameDataService gameDataService;
	@Autowired private AdminSpService adminSpService;
	@Autowired private CompanyService companyService;
	
	@RequestMapping("gameList")
	public String list(HttpServletRequest request,HttpServletResponse response) {
		
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		date.setDate(date.getDate()-1);
		
		StringBuilder condition = new StringBuilder(" where ");
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("开始时间")) {
			startTime = DateUtil.formatDate(date);
		}
		condition.append(" dt >= '"+startTime+"'");
		url.append("&txtDateStart="+startTime);
		
		String endTime = request.getParameter("txtDateEnd");
		if(endTime==null||endTime.length()==0||endTime.equals("结束时间")) {
			endTime = DateUtil.formatDate(date);
		}
		condition.append(" and dt <= '"+endTime+"'");
		url.append("&txtDateEnd="+endTime);
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and spid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and spid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and cpid = "+cpId);
		}
		
		condition.append(" order by dt desc");
		
		List<GameDataBean> list = this.gameDataService.findDataByCondition(condition.toString());
		
		int totalCount =  list.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<GameDataBean> pageBean = new PageBean<GameDataBean>(totalCount, currentPage,20);
		//List<GameDataBean> blist = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), list);
		
		
		
		
		GameDataBean all = new GameDataBean();
		
		for (GameDataBean gameDataBean : list) {
			AdminUserBean cpBean = adminUserService.findById(gameDataBean.getCpid());
			if(cpBean==null) {
				//cpBean = new AdminUserBean();
			} else {
				AdminSpBean spBean = adminSpService.findAdminSpById(gameDataBean.getSpid());
				gameDataBean.setCpname(cpBean.getCpname());
				gameDataBean.setCpid(gameDataBean.getCpid());
				gameDataBean.setId(gameDataBean.getId());
				gameDataBean.setData1(gameDataBean.getData1());
				all.setData1(gameDataBean.getData1()+all.getData1());
				gameDataBean.setData2(gameDataBean.getData2());
				all.setData2(gameDataBean.getData2()+all.getData2());
				gameDataBean.setData3(gameDataBean.getData3());
				all.setData3(gameDataBean.getData3()+all.getData3());
				gameDataBean.setData4(gameDataBean.getData4());
				all.setData4(gameDataBean.getData4()+all.getData4());
				
				
				gameDataBean.setCpdata1(gameDataBean.getCpdata1());
				all.setCpdata1(all.getCpdata1()+gameDataBean.getCpdata1());
				gameDataBean.setCpdata2(gameDataBean.getCpdata2());
				all.setCpdata2(all.getCpdata2()+gameDataBean.getCpdata2());
				gameDataBean.setCpdata3(gameDataBean.getCpdata3());
				all.setCpdata3(all.getCpdata3()+gameDataBean.getCpdata3());
				gameDataBean.setCpdata4(gameDataBean.getCpdata4());
				all.setCpdata4(all.getCpdata4()+gameDataBean.getCpdata4());
				gameDataBean.setFeePercent(gameDataBean.getData1()<=0?0:gameDataBean.getData2()*100/gameDataBean.getData1());
				gameDataBean.setAvgUser(gameDataBean.getData2()<=0?0:(gameDataBean.getData4()/2)*100/gameDataBean.getData2());
				if(spBean!=null) {
					gameDataBean.setSpname(spBean.getSpname());
				} else {
					gameDataBean.setSpname("不详");
				}
				
			}
			
		}
		
		List<GameDataBean> resultList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), list);
		
		
		all.setFeePercent(all.getData1()<=0?0:all.getData2()*100/all.getData1());
		all.setAvgUser(all.getData2()<=0?0:(all.getData4()/2)*100/all.getData2());
		/*
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		StringBuffer url = new StringBuffer("?1=1");
		int cpid = BaseUtil.stringToInt(request.getParameter("cpid"));
		
		url.append("&cpid="+cpid);
		
		int totalCount =  this.gameDataService.findDataCountByCpid(cpid);
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		
		PageBean<GameDataBean> pageBean = new PageBean<GameDataBean>(totalCount, currentPage,20);
		
		List<GameDataBean> list = this.gameDataService.findDataByCpid(cpid, pageBean.getStart(), pageBean.getPageSize());
		
		List<GameDataBean> resultList = new ArrayList<GameDataBean>();
		
		
		adminSpService.findList(0, 1000);
		
		for (GameDataBean gameDataBean : list) {
			AdminUserBean cpBean = adminUserService.findById(gameDataBean.getCpid());
			if(cpBean==null) {
				//cpBean = new AdminUserBean();
			} else {
				AdminSpBean spBean = adminSpService.findAdminSpById(gameDataBean.getSpid());
				gameDataBean.setCpname(cpBean.getCpname());
				gameDataBean.setCpid(gameDataBean.getCpid());
				gameDataBean.setId(gameDataBean.getId());
				gameDataBean.setData1(gameDataBean.getData1());
				gameDataBean.setData2(gameDataBean.getData2());
				gameDataBean.setData3(gameDataBean.getData3());
				gameDataBean.setData4(gameDataBean.getData4());
				gameDataBean.setCpdata1(gameDataBean.getData1()*(100-cpBean.getCppercent())/100);
				gameDataBean.setCpdata2(gameDataBean.getData2()*(100-cpBean.getCppercent())/100);
				gameDataBean.setCpdata3(gameDataBean.getData3()*(100-cpBean.getCppercent())/100);
				gameDataBean.setCpdata4(((gameDataBean.getData4()*(100-cpBean.getCppercent())/100)/2)*2);
				gameDataBean.setFeePercent(gameDataBean.getData1()<=0?0:gameDataBean.getData2()*100/gameDataBean.getData1());
				gameDataBean.setAvgUser(gameDataBean.getData2()<=0?0:(gameDataBean.getData4()/2)*100/gameDataBean.getData2());
				if(spBean!=null) {
					gameDataBean.setSpname(spBean.getSpname());
				} else {
					gameDataBean.setSpname("不详");
				}
				
				resultList.add(gameDataBean);
			}
			
		}
		
		*/
		
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(resultList);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("all", all);
		
		return "gameList";
	}
	
	
	@RequestMapping("cpGameList")
	public String cpGgameList(HttpServletRequest request,HttpServletResponse response) {
		
		AdminUserBean cpBean = (AdminUserBean) request.getSession().getAttribute("adminUser");
		
		StringBuffer url = new StringBuffer("?1=1");
		
		
		int totalCount =  this.gameDataService.findDataCountByCpid(cpBean.getId());
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		
		PageBean<GameDataBean> pageBean = new PageBean<GameDataBean>(totalCount, currentPage,10);
		
		List<GameDataBean> list = this.gameDataService.findDataByCpid(cpBean.getId(), pageBean.getStart(), pageBean.getPageSize());
		
		List<GameDataBean> resultList = new ArrayList<GameDataBean>();
		
		for (GameDataBean gameDataBean : list) {
				gameDataBean.setCpname(cpBean.getCpname());
				gameDataBean.setCpid(gameDataBean.getCpid());
				gameDataBean.setId(gameDataBean.getId());
				gameDataBean.setData1(gameDataBean.getData1());
				gameDataBean.setData2(gameDataBean.getData2());
				gameDataBean.setData3(gameDataBean.getData3());
				gameDataBean.setData4(gameDataBean.getData4());
				gameDataBean.setCpdata1(gameDataBean.getCpdata1());
				gameDataBean.setCpdata2(gameDataBean.getCpdata2());
				gameDataBean.setCpdata3(gameDataBean.getCpdata3());
				gameDataBean.setCpdata4(gameDataBean.getCpdata4());
				gameDataBean.setFeePercent(gameDataBean.getData1()<=0?0:gameDataBean.getData2()*100/gameDataBean.getData1());
				gameDataBean.setAvgUser(gameDataBean.getData2()<=0?0:(gameDataBean.getData4()/2)*100/gameDataBean.getData2());
				resultList.add(gameDataBean);
		}
		
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(resultList);
		request.setAttribute("pageBean", pageBean);
		
		return "cpGameList";
	}
	
	
	
	
	
	@RequestMapping("videoList")
	public String videoList(HttpServletRequest request,HttpServletResponse response) {
		
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		date.setDate(date.getDate()-1);
		
		StringBuilder condition = new StringBuilder(" where ");
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("开始时间")) {
			startTime = DateUtil.formatDate(date);
		}
		condition.append(" dt >= '"+startTime+"'");
		url.append("&txtDateStart="+startTime);
		
		String endTime = request.getParameter("txtDateEnd");
		if(endTime==null||endTime.length()==0||endTime.equals("结束时间")) {
			endTime = DateUtil.formatDate(date);
		}
		condition.append(" and dt <= '"+endTime+"'");
		url.append("&txtDateEnd="+endTime);
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and spid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and spid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and cpid = "+cpId);
		}
		
		condition.append(" order by dt desc");
		
		List<GameDataBean> list = this.gameDataService.findVideoByCondition(condition.toString());
		
		int totalCount =  list.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<GameDataBean> pageBean = new PageBean<GameDataBean>(totalCount, currentPage,20);
		//List<GameDataBean> blist = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), list);
		
		
		
		
		GameDataBean all = new GameDataBean();
		
		for (GameDataBean gameDataBean : list) {
			AdminUserBean cpBean = adminUserService.findById(gameDataBean.getCpid());
			if(cpBean==null) {
				//cpBean = new AdminUserBean();
			} else {
				AdminSpBean spBean = adminSpService.findAdminSpById(gameDataBean.getSpid());
				gameDataBean.setCpname(cpBean.getCpname());
				gameDataBean.setCpid(gameDataBean.getCpid());
				gameDataBean.setId(gameDataBean.getId());
				gameDataBean.setData1(gameDataBean.getData1());
				all.setData1(gameDataBean.getData1()+all.getData1());
				gameDataBean.setData2(gameDataBean.getData2());
				all.setData2(gameDataBean.getData2()+all.getData2());
				gameDataBean.setData3(gameDataBean.getData3());
				all.setData3(gameDataBean.getData3()+all.getData3());
				gameDataBean.setData4(gameDataBean.getData4());
				all.setData4(gameDataBean.getData4()+all.getData4());
				
				
				gameDataBean.setCpdata1(gameDataBean.getCpdata1());
				all.setCpdata1(all.getCpdata1()+gameDataBean.getCpdata1());
				gameDataBean.setCpdata2(gameDataBean.getCpdata2());
				all.setCpdata2(all.getCpdata2()+gameDataBean.getCpdata2());
				gameDataBean.setCpdata3(gameDataBean.getCpdata3());
				all.setCpdata3(all.getCpdata3()+gameDataBean.getCpdata3());
				gameDataBean.setCpdata4(gameDataBean.getCpdata4());
				all.setCpdata4(all.getCpdata4()+gameDataBean.getCpdata4());
				gameDataBean.setFeePercent(gameDataBean.getData1()<=0?0:gameDataBean.getData2()*100/gameDataBean.getData1());
				gameDataBean.setAvgUser(gameDataBean.getData2()<=0?0:(gameDataBean.getData4()/2)*100/gameDataBean.getData2());
				if(spBean!=null) {
					gameDataBean.setSpname(spBean.getSpname());
				} else {
					gameDataBean.setSpname("不详");
				}
				
				//resultList.add(gameDataBean);
			}
			
		}
		List<GameDataBean> resultList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), list);
		all.setFeePercent(all.getData1()<=0?0:all.getData2()*100/all.getData1());
		all.setAvgUser(all.getData2()<=0?0:(all.getData4()/2)*100/all.getData2());
		
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(resultList);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("all", all);
		
		return "videoList";
	}
	
	
	@RequestMapping("cpVideoList")
	public String cpVideoList(HttpServletRequest request,HttpServletResponse response) {
		
		AdminUserBean cpBean = (AdminUserBean) request.getSession().getAttribute("adminUser");
		
		StringBuffer url = new StringBuffer("?1=1");
		
		
		int totalCount =  this.gameDataService.findVideoCountByCpid(cpBean.getId());
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		
		PageBean<GameDataBean> pageBean = new PageBean<GameDataBean>(totalCount, currentPage,10);
		
		List<GameDataBean> list = this.gameDataService.findVideoByCpid(cpBean.getId(), pageBean.getStart(), pageBean.getPageSize());
		
		List<GameDataBean> resultList = new ArrayList<GameDataBean>();
		
		for (GameDataBean gameDataBean : list) {
				gameDataBean.setCpname(cpBean.getCpname());
				gameDataBean.setCpid(gameDataBean.getCpid());
				gameDataBean.setId(gameDataBean.getId());
				gameDataBean.setData1(gameDataBean.getData1());
				gameDataBean.setData2(gameDataBean.getData2());
				gameDataBean.setData3(gameDataBean.getData3());
				gameDataBean.setData4(gameDataBean.getData4());
				gameDataBean.setCpdata1(gameDataBean.getCpdata1());
				gameDataBean.setCpdata2(gameDataBean.getCpdata2());
				gameDataBean.setCpdata3(gameDataBean.getCpdata3());
				gameDataBean.setCpdata4(gameDataBean.getCpdata4());
				gameDataBean.setFeePercent(gameDataBean.getData1()<=0?0:gameDataBean.getData2()*100/gameDataBean.getData1());
				gameDataBean.setAvgUser(gameDataBean.getData2()<=0?0:(gameDataBean.getData4()/2)*100/gameDataBean.getData2());
				resultList.add(gameDataBean);
		}
		
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(resultList);
		request.setAttribute("pageBean", pageBean);
		
		return "cpVideoList";
	}
	
}
